package com.yanfan.web.controller.system;

import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.core.domain.model.RegisterBody;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.framework.web.service.SysRegisterService;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.domain.DeviceShare;
import com.yanfan.iot.domain.Product;
import com.yanfan.iot.mapper.DeviceMapper;
import com.yanfan.iot.mapper.ProductMapper;
import com.yanfan.iot.model.ThingsModelPerm;
import com.yanfan.iot.service.IDeviceShareService;
import com.yanfan.iot.service.IThingsModelService;
import com.yanfan.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@Api(tags = "注册账号")
@RestController
public class SysRegisterController extends BaseController {
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IThingsModelService thingsModelService;

    @Autowired
    private IDeviceShareService deviceShareService;

    @ApiOperation("注册账号")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user) {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return error("当前系统没有开启注册功能！");
        }
        //完成用户注册
        String msg = registerService.register(user);
        //当sys.device.sbfx为false时,走用户注册后完成设备的分配
        if (!("true".equals(configService.selectConfigByKey("sys.device.sbfx")))) {
            //需要的结构
            DeviceShare deviceShare = new DeviceShare();
            deviceShare.setPhonenumber(user.getPhonenumber());
            deviceShare.setDelFlag("0");
            SysUser users = registerService.getuser(user.getPhonenumber());
            deviceShare.setUserId(users.getUserId());
            //获取客户测试下面的所有机构
            Device device = new Device();
            device.setTenantId(9L);
            List<Device> deviceslist = deviceMapper.selectDeviceList(device);
            for (Device devices : deviceslist) {
                //获取对应的产品类型
                Product product = productMapper.selectProductByProductId(devices.getProductId());
                if (product == null) {
                    continue;
                }
                deviceShare.setDeviceId(devices.getDeviceId());
                List<ThingsModelPerm> list = thingsModelService.selectThingsModelPermList(product.getProductId());
                String substrings = "ota,timer,log,monitor,statistic";
                if (list != null && list.size() > 0) {
                    String identifiers = list
                            .stream()
                            .filter(item -> item.getIdentifier() != null)
                            .map(ThingsModelPerm::getIdentifier)
                            .collect(Collectors.joining(","));
                    substrings = substrings + "," + identifiers;
                }
                deviceShare.setPerms(substrings);
                deviceShareService.insertDeviceShare(deviceShare);
            }
        }
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
}
